package com.wswy.wzcx.ui.car.fine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.format.DateFormat
import android.view.View
import com.amap.api.maps2d.CameraUpdateFactory
import com.amap.api.maps2d.model.BitmapDescriptorFactory
import com.amap.api.maps2d.model.LatLng
import com.amap.api.maps2d.model.MarkerOptions
import com.blankj.utilcode.util.SizeUtils
import com.wswy.wzcx.R
import com.wswy.wzcx.model.TrafficViolationInfo
import com.wswy.wzcx.model.home.RPaths
import com.wswy.wzcx.model.home.RouterUtils
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.module.InternalMsgCodeKt
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.Constants
import com.wswy.wzcx.ui.activity.SampleBaseActivity
import com.wswy.wzcx.ui.other.CloseDelegate
import com.wswy.wzcx.utils.ImageUtils
import com.wswy.wzcx.utils.UiUtils
import kotlinx.android.synthetic.main.activity_query_result_detail.*

class QueryResultDetailActivity : SampleBaseActivity() {

    companion object {

        private const val ARG_DATA = "args.info"


        fun start(context: Context,info: TrafficViolationInfo,userCarInfo: UserCarInfo?){

            Intent(context,QueryResultDetailActivity::class.java).apply {
                putExtra(ARG_DATA,info)
                putExtra(Constants.EXTRA_USER_CAR_INFO,userCarInfo)
            }.run {
                context.startActivity(this)
            }

        }
    }


    private var  delegate: CloseDelegate?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_query_result_detail)
        bd_mapView.onCreate(savedInstanceState)

        delegate = CloseDelegate(this,InternalMsgCodeKt.UPLOAD_COMPLETE_WDZD)

        val violationInfo = intent?.getParcelableExtra<TrafficViolationInfo>(ARG_DATA)

        setAppTitle("详情")

        if(violationInfo == null){
            finish()
            return
        }

        circle_line.attachAnchorViews(tv_fine_addr,tv_query_time,tv_fine_time)

        fullData(violationInfo,intent?.getParcelableExtra<UserCarInfo?>(Constants.EXTRA_USER_CAR_INFO))

        if(violationInfo.lon == 0.0 || violationInfo.lat == 0.0){
            //隐藏地图
            bd_mapView.visibility = View.GONE
//            val lp = ll_bottom.layoutParams as RelativeLayout.LayoutParams
//
//            lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0)
//            lp.addRule(RelativeLayout.BELOW, R.id.ll_top)

        }else{
            //显示地图
            val lat = violationInfo.lat
            val lon = violationInfo.lon

            bd_mapView.visibility = View.VISIBLE

            val aMap = bd_mapView.map
            aMap.uiSettings.zoomPosition
            val latLng = LatLng(lat, lon)

            aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    latLng, 16f))

            //aMap.animateCamera()
            aMap.moveCamera(CameraUpdateFactory.scrollBy(0f, SizeUtils.dp2px(100f).toFloat() ))

            aMap.uiSettings.isZoomControlsEnabled = false


            val mCurrentMarker = BitmapDescriptorFactory.fromBitmap(ImageUtils.getMapMarkBitmap())

            val options = MarkerOptions()
            options.icon(mCurrentMarker)
            options.anchor(0.5f, 0.5f)
            options.position(latLng)

            aMap.addMarker(options)

        }

    }


    private fun fullData(info:TrafficViolationInfo,userCarInfo: UserCarInfo?){
        tv_car_no.text = info.carNo

        tv_query_time.text = getString(R.string.detail_query_time,DateFormat.format("yyyy-MM-dd HH:mm:ss",info.createTime * 1000))

        tv_fine_time.text = getString(R.string.detail_fine_time,info.fineTime)
        tv_fine_addr.text = info.fineLocation
        tv_fine_desc.text = info.fineDetail


        if (info.statusName == "10"){
            btn_daiban.text = "帮我代办"
            btn_daiban.isEnabled = true
        }else{
            btn_daiban.text = info.getStatusNameDesc(this)
            btn_daiban.isEnabled = false
        }



        tv_fine_points.text = getString(R.string.detail_points,info.fineDeductPoints,info.fineFee)

        img_car_icon.setImageURI(userCarInfo?.carExtendInfo?.carTypeData?.series?.logo)

        UiUtils.addOnGlobalLayoutListener(ll_bottom){

           if (it.height > 0){

               shadow_view.layoutParams?.run {
                   height = it.height
                   shadow_view.layoutParams = this
               }

               shadow_view.visibility = View.VISIBLE
           }
        }


        btn_daiban.setOnClickListener {

            RouterUtils.getTargetIntent(this,RPaths.wzdbChooseCar)?.let {
                it.putExtra(Constants.EXTRA_DATA, userCarInfo)
                it.putExtra("defaultChoose",info.uniqueMd5Id)
                startActivity(it)
            }

            StatTools.sendClick(applicationContext,StatisticsId.detail_single_commission)

        }

    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        bd_mapView.onSaveInstanceState(outState)
    }

    override fun onResume() {
        super.onResume()

        bd_mapView.onResume()

    }

    override fun onPause() {
        super.onPause()
        bd_mapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        bd_mapView.onDestroy()
        delegate?.onDestroy()

    }

}